from zope.interface import implements, Interface
from zope.component import getMultiAdapter
from Acquisition import aq_inner

from Products.Five import BrowserView
from Products.CMFCore.utils import getToolByName

from plone.memoize.instance import memoize

from shema.archetype import archetypeMessageFactory as _


class Ipag_inicialView(Interface):
    """
    pag_inicial view interface
    """

    def test():
        """ test method"""


class pag_inicialView(BrowserView):
    """
    pag_inicial browser view
    """
    implements(Ipag_inicialView)

    def __init__(self, context, request):
        self.context = context
        self.request = request

    @property
    def portal_catalog(self):
        return getToolByName(self.context, 'portal_catalog')

    @property
    def portal(self):
        return getToolByName(self.context, 'portal_url').getPortalObject()

    def test(self):
        """
        test method
        """
        dummy = _(u'a dummy string')

        return {'dummy': dummy}

    # palavra pastoral
    def published_palavra_pastoral(self):
        return self._data()

    def all_palavra_pastoral_link(self):
        portal_state = getMultiAdapter((self.context, self.request), name=u'plone_portal_state')
        portal_url = portal_state.portal_url()
        portal = portal_state.portal()
        
        if 'palavra-pastoral' in portal.objectIds():
            return '%s/palavra-pastoral' % portal_url
        else:
            return None

    def retorna_ultima_palavrapastoral(self):
        """
        retorna a ultima palavra pastoral publicadas por ordem
        decrescente de data de efetivacao
        """
        results = self.portal_catalog.searchResults(Type=['Palavra Pastoral',],
                                                       review_state = "published",
                                                       sort_order = "reverse",
                                                       sort_limit = 1,
                                                       sort_on="effective")
        return results


    @memoize
    def _data(self):
        context = aq_inner(self.context)
        catalog = getToolByName(context, 'portal_catalog')
        return catalog(portal_type='Palavra Pastoral',
                       sort_on='Date',
                       sort_order='reverse',
                       sort_limit=3)


    # noticias

    def all_noticias_link(self):
        portal_state = getMultiAdapter((self.context, self.request), name=u'plone_portal_state')
        portal_url = portal_state.portal_url()
        portal = portal_state.portal()
        
        if 'news' in portal.objectIds():
            return '%s/news' % portal_url
        else:
            return None

    def retorna_ultima_noticia(self):
        """
        retorna as ultimas duas noticias publicadas por ordem
        decrescente de data de efetivacao
        """
        results = self.portal_catalog.searchResults(Type=['News Item',],
                                                       review_state = "published",
                                                       sort_order = "reverse",
                                                       sort_limit = 1,
                                                       sort_on="effective")
        return results

    def published_noticias(self):
        """
        retorna da terceiras a setima ultimas noticias publicadas por ordem
        decrescente de data de efetivacao
        """
        results = self.portal_catalog.searchResults(Type=['News Item',],
                                                       review_state = "published",
                                                       sort_order = "reverse",
                                                       sort_limit = 4,
                                                       sort_on="effective")[1:4]
        return results

    # avisos
    def retorna_avisos(self):
        results = self.context.portal_catalog.searchResults(path = "/shema/avisos",
                                                            Type='Page',
                                                            review_state="published",
                                                            sort_order="descending",
                                                            sort_on="effective",
                                                            sort_limit=3)
        return results

    def all_avisos_link(self):
        portal_state = getMultiAdapter((self.context, self.request), name=u'plone_portal_state')
        portal_url = portal_state.portal_url()
        portal = portal_state.portal()
        
        if 'avisos' in portal.objectIds():
            return '%s/avisos' % portal_url
        else:
            return None

    # artigos
    def retorna_artigos(self):
        results = self.context.portal_catalog.searchResults(path = "/shema/artigos",
                                                            Type='Page',
                                                            review_state="published",
                                                            sort_order="descending",
                                                            sort_on="effective",
                                                            sort_limit=3)
        return results

    def all_artigos_link(self):
        portal_state = getMultiAdapter((self.context, self.request), name=u'plone_portal_state')
        portal_url = portal_state.portal_url()
        portal = portal_state.portal()
        
        if 'artigos' in portal.objectIds():
            return '%s/artigos' % portal_url
        else:
            return None
